import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

//实现一个线程池
class MyThreadPool{
    //阻塞队列用于存放任务
    private BlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();

    public void submit(Runnable runnable) throws InterruptedException {
        queue.put(runnable);
    }

    //实现一个固定线程数的线程池
    public MyThreadPool(int n){
        for (int i = 0;i < n;i++){
            Thread t = new Thread(()->{
               try {
                   while (true){
                       //此处需要让线程池内部有个while循环，不停地取任务
                       Runnable runnable = queue.take();
                       runnable.run();
                   }
               }catch (InterruptedException e){
                   e.printStackTrace();
               }
            });
            t.start();
        }
    }
}
public class ThreadDemo {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPool pool = new MyThreadPool(10);
        for (int i = 0; i < 1000; i++) {
            int number = i;
            pool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("hello" + number);
                }
            });
            Thread.sleep(1000);
        }
    }
}
